<!-- src/components/Login.vue -->
<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>登录</span>
      </div>
    </template>
    <el-form :model="form" ref="loginForm" label-width="100px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { useAuthStore } from '../stores/authStore'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()

const authStore = useAuthStore()

const form = reactive({
  username: '',
  password: '',
})

const handleLogin = () => {
  authStore.login(form.username, form.password)

  // 假设登录成功后 user 不为 null
  if (authStore.user) {
    ElMessage.success('登录成功！')
    router.push('/') // 跳转到首页
  } else {
    ElMessage.error('登录失败，请检查用户名密码')
  }
  // ElMessage.success('登录请求已发送')
}
</script>

<style scoped>
.box-card {
  width: 400px;
  margin: auto;
  margin-top: 100px;
}
</style>
